package com.dinadi.yidi.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.MyCouponsStateRequestEntity;
import com.dinadi.yidi.utils.FormatCurrentData;
import com.dinadi.yidi.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * author: JiaYu.He
 * Date: 2017/9/20
 * Time: 14:32
 */
public class CouponCenterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private Activity mContext;

    private List<MyCouponsStateRequestEntity> mDatas = new ArrayList<>();
    private MyCouponsStateRequestEntity bean;
    /*运用典型的观察者模式添加点击事件*/
    /*定义接口变量*/
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;


    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, MyCouponsStateRequestEntity date);
    }

    public void addAllData(List<MyCouponsStateRequestEntity> dataList) {
        this.mDatas.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
    }

    public CouponCenterAdapter(Activity context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_my_coupons_state_item, parent, false);
        //将创建的View注册点击事件
        v.setOnClickListener(this);
        return new ItemViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        itemViewHolder.tv_coupon_name.setText(mDatas.get(position).getTitle());
        itemViewHolder.tv_money.setText("￥" + mDatas.get(position).getAccount());
        itemViewHolder.tv_full_account.setText("满" + mDatas.get(position).getFullAccount() + "可用");
        String startDate = FormatCurrentData.getDate(mDatas.get(position).getEffectiveTime());
        String endDate = FormatCurrentData.getDate(mDatas.get(position).getExpirationTime());
        itemViewHolder.term_of_validity.setText(startDate + " - " + endDate);
        String isReceive = mDatas.get(position).getIsReceive();
        String isReceiveTime = mDatas.get(position).getIsReceiveTime();
        String id = mDatas.get(position).getId();
        bean.setId(id);
        bean.setIsReceive(isReceive);
        bean.setIsReceiveTime(isReceiveTime);
        if ("0".equals(mDatas.get(position).getIsReceive())) {
            /*已领取*/
            itemViewHolder.icon_conpous_already_in_use.setVisibility(View.VISIBLE);
            itemViewHolder.icon_coupons_available.setImageResource(R.drawable.icon_coupons_unavailable);
            itemViewHolder.coupon_full_court.setImageResource(R.drawable.icon_coupon_full_court_no);
        }
        holder.itemView.setTag(bean);
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView tv_coupon_name, tv_money, tv_full_account, term_of_validity;
        RelativeLayout item_layout;
        ImageView icon_coupons_available, coupon_full_court, icon_conpous_already_in_use;


        public ItemViewHolder(View itemView) {
            super(itemView);
            tv_coupon_name = (TextView) itemView.findViewById(R.id.tv_coupon_name);
            tv_money = (TextView) itemView.findViewById(R.id.tv_money);
            tv_full_account = (TextView) itemView.findViewById(R.id.tv_full_account);
            term_of_validity = (TextView) itemView.findViewById(R.id.term_of_validity);
            item_layout = (RelativeLayout) itemView.findViewById(R.id.item_layout);
            icon_coupons_available = (ImageView) itemView.findViewById(R.id.icon_coupons_available);
            coupon_full_court = (ImageView) itemView.findViewById(R.id.coupon_full_court);
            icon_conpous_already_in_use = (ImageView) itemView.findViewById(R.id.icon_conpous_already_in_use);
            bean = new MyCouponsStateRequestEntity();

        }


    }

    /*将事件转移给外面的调用者*/
    @Override
    public void onClick(View v) {

        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (MyCouponsStateRequestEntity) v.getTag());

        }
    }

    /*最后暴露给外面的调用者*/
    public void setmOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;

    }

}
